import React, {Component} from 'react';

import Child  from "./child";

class Parent extends Component {

    state={
        hasError:''//用于标识子组件是否产生错误
    }

    // 当Parent的子组件出现报错时候，会触发getDerivedstateFromError调用，并携带错误信息
    static getDerivedStateFromError(error){
        console.log('@@@',error)
        return{hasError:error}
    }

    componentDidCatch(error, errorInfo) {
        console.log('统计错误出处，反馈给服务器，用于通知编码人员的bug解决')
        console.log(error,errorInfo)
    }

    render() {
        return (
            <div>
                <h1>我是父组件</h1>
                {this.state.hasError===''? <Child></Child>:<h1>当前网络不稳定</h1>}
            </div>
        );
    }
}

export default Parent;